#!/bin/sh
# PCP QA Test No. 1653
# Checks for pmda-denki
#
# Copyright (c) 2021 Christian Horn.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

[ $PCP_PLATFORM = linux ] || _notrun "Linux-specific kernel metrics"

_cleanup()
{
    cd $here
    $sudo rm -rf $PCP_SYSCONF_DIR/labels/*
    _restore_config $PCP_SYSCONF_DIR/labels
    _sighup_pmcd
    $sudo cp $tmp.install $PCP_PMDAS_DIR/denki/Install
    _cleanup_pmda denki
    $sudo rm -rf $tmp $tmp.*
}

status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

userid=`id -u`
groupid=`id -g`
hostname=`hostname`
machineid=`_machine_id`
domainname=`_domain_name`

_filter_labels()
{
    sed \
	-e "s/\(\"userid\"\):$userid\([^0-9]\)/\1:USERID\2/g" \
	-e "s/\(\"groupid\"\):$groupid\([^0-9]\)/\1:GROUPID\2/g" \
	-e "s/\(\"hostname\"\):\"$hostname\"/\1:\"HOSTNAME\"/g" \
	-e "s/\(\"machineid\"\):\"$machineid\"/\1:\"MACHINEID\"/g" \
	-e "s/\(\"domainname\"\):\"$domainname\"/\1:\"DOMAINNAME\"/g" \
    | _filter_optional_labels
}

_filter_instnames()
{
    sed \
	-e 's/[0-9][0-9]* or \"battery-[0-9][0-9]*/N or BATTERY-N/g'
    # end
}

_filter()
{
    sed \
	-e "s,$here,PWD,g" \
	-e "s,$PCP_PMDAS_DIR,PCP_PMDAS_DIR,g" \
    # end
}

# real QA test starts here
_save_config $PCP_SYSCONF_DIR/labels
$sudo rm -rf $PCP_SYSCONF_DIR/labels/*
$sudo mkdir $PCP_SYSCONF_DIR/labels/optional

cd $here/denki
datasets=`LC_COLLATE=POSIX ls -1 -r *.tgz | sed -e 's/\.tgz$//g'`

echo
echo "### Extracting test datasets..."
echo
for dataset in $datasets
do
    tar xzf $dataset.tgz
done

_prepare_pmda denki
$sudo cp $PCP_PMDAS_DIR/denki/Install $tmp.install

for dataset in $datasets
do
    echo
    echo "### Installing test dataset $dataset ..."
    echo
    cd $PCP_PMDAS_DIR/denki
    ROOTPATH="$here/denki/$dataset"
    $sudo sed -ie \
	"s,^args.*,args=\"-U root -r ${ROOTPATH}\","  \
	Install
    $sudo ./Install 2>&1
    echo $dataset >> $seq_full
    pminfo -dfFl denki \
	    | tee -a $seq_full \
	    | $here/src/sortinst -v \
	    | _filter_instnames \
	    | _filter_labels \
    #end
done

echo
echo "### Removing test datasets..."
echo
for dataset in $datasets
do
    rm -fr $here/denki/$dataset
done

# success, all done
exit
